<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="购车人" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入购车人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="购车id" prop="goodsId">
        <el-input
          v-model="queryParams.goodsId"
          placeholder="请输入购车id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单状态" prop="currentPath">
        <el-input
          v-model="queryParams.currentPath"
          placeholder="请输入订单状态"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="微信用户id" prop="wxUserId" label-width="88px">
        <el-input
          v-model="queryParams.wxUserId"
          placeholder="请输入微信用户id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['car:order:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['car:order:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['car:order:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['car:order:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="订单id" align="center" prop="id"/>
      <el-table-column label="购车人" align="center" prop="name"/>
      <el-table-column label="联系方式" align="center" prop="phone"/>
      <el-table-column label="购车id" align="center" prop="goodsId"/>
      <el-table-column label="所购车辆" align="center" prop="goodsName"/>
      <el-table-column label="身份证号码" align="center" prop="card"/>
      <el-table-column label="支付时间" align="center" prop="payDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.payDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="支付金额" align="center" prop="total"/>
      <el-table-column label="购车方式" align="center" prop="way"/>
      <el-table-column label="征信报告" align="center" prop="zheng" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.zheng" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="银行流水" align="center" prop="water" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.water" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="结婚证/单身证" align="center" prop="hun" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.hun" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="身份证(正面)" align="center" prop="cardFront" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.cardFront" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="身份证(反面)" align="center" prop="cardBack" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.cardBack" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <!--      <el-table-column label="订单流程" align="center" prop="allPath"/>-->
      <el-table-column label="订单状态" align="center" prop="currentPathDesc"/>
      <el-table-column label="微信用户id" align="center" prop="wxUserId"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['car:order:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['car:order:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="购车人" prop="name">
          <el-input v-model="form.name" placeholder="请输入购车人"/>
        </el-form-item>
        <el-form-item label="联系方式" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入联系方式"/>
        </el-form-item>
        <!--        <el-form-item label="购车id" prop="goodsId">-->
        <!--          <el-input v-model="form.goodsId" placeholder="请输入购车id" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="所购车辆" prop="goodsName">-->
        <!--          <el-input v-model="form.goodsName" placeholder="请输入所购车辆" />-->
        <!--        </el-form-item>-->
        <el-form-item label="所购车辆" prop="goodsId">
          <el-select v-model="form.goodsId" placeholder="请选择所购车辆" filterable clearable>
            <el-option
              v-for="item in goodsList"
              :key="item.id"
              :label="item.goodsName"
              :value="item.id"
              clearable
            >
              {{ item.goodsName }}
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="身份证号码" prop="card">
          <el-input v-model="form.card" placeholder="请输入身份证号码"/>
        </el-form-item>
        <el-form-item label="支付时间" prop="payDate">
          <el-date-picker clearable
                          v-model="form.payDate"
                          type="datetime"
                          value-format="yyyy-MM-dd hh:mm:ss"
                          placeholder="请选择支付时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="支付金额" prop="total">
          <el-input v-model="form.total" placeholder="请输入支付金额"/>
        </el-form-item>
        <!--        <el-form-item label="购车方式" prop="way">-->
        <!--          <el-input v-model="form.way" placeholder="请输入购车方式" />-->
        <!--        </el-form-item>-->
        <el-form-item label="购车方式" prop="way">
          <el-select @change="selectWayChanged" v-model="form.way" placeholder="请选择购车方式" clearable>
            <el-option
              v-for="dict in wayDict"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="征信报告" prop="zheng">
          <image-upload v-model="form.zheng" :limit="1"/>
        </el-form-item>
        <el-form-item label="银行流水" prop="water">
          <image-upload v-model="form.water" :limit="1"/>
        </el-form-item>
        <el-form-item label="结婚证/单身证" prop="hun">
          <image-upload v-model="form.hun" :limit="1"/>
        </el-form-item>
        <el-form-item label="身份证(正面)" prop="cardFront">
          <image-upload v-model="form.cardFront" :limit="1"/>
        </el-form-item>
        <el-form-item label="身份证(反面)" prop="cardBack">
          <image-upload v-model="form.cardBack" :limit="1"/>
        </el-form-item>
        <el-form-item label="订单流程" prop="allPath" hidden="hidden">
          <el-input v-model="form.allPath" placeholder="请输入订单流程"/>
        </el-form-item>
        <!--        <el-form-item label="订单状态" prop="currentPath">-->
        <!--          <el-input v-model="form.currentPath" placeholder="请输入订单状态"/>-->
        <!--        </el-form-item>-->
        <el-form-item label="订单状态" prop="currentPath">
          <el-select v-model="form.currentPath" placeholder="请选择订单状态" filterable clearable>
            <el-option
              v-for="item in currentPathList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
              clearable
            >
              {{ item.label }}
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="微信用户id" prop="wxUserId">
          <el-input v-model="form.wxUserId" placeholder="请输入微信用户id"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addOrder, delOrder, getOrder, listOrder, updateOrder} from "@/api/car/order";
import {listGoodsAllVo} from "@/api/car/goods";

export default {
  name: "Order",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单管理表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        goodsId: null,
        currentPath: null,
        wxUserId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      wayDict: [
        {label: "贷款", value: "贷款"},
        {label: "全款", value: "全款"},
      ],
      //车辆商品下拉列表
      goodsList: [],
      //全款
      allPathFull: [
        {
          text: '支付定金',
        },
        {
          text: '生产中',
        },
        {
          text: '车辆到店',
        },
        {
          text: '车辆交付',
        }],
      //贷款
      allPathLoan: [
        {
          text: '贷款申请',
        },
        {
          text: '贷款通过',
        },
        {
          text: '生产中',
        },
        {
          text: '车辆到店',
        },
        {
          text: '车辆交付',
        }
      ],
      currentPathList: [],
      //全款订单状态列表
      currentPathListFull: [
        {label: "支付定金（全款）", value: "0"},
        {label: "生产中（全款）", value: "1"},
        {label: "车辆到店（全款）", value: "2"},
        {label: "车辆交付（全款）", value: "3"},
      ],
      //贷款订单状态列表
      currentPathListLoan: [
        {label: "贷款申请（贷款）", value: "0"},
        {label: "贷款通过（贷款）", value: "1"},
        {label: "生产中（贷款）", value: "2"},
        {label: "车辆到店（贷款）", value: "3"},
        {label: "车辆交付（贷款）", value: "4"},
      ]
    };
  },
  created() {
    this.getList();
    this.handlerGoodsAllVo();
  },
  methods: {
    /** 查询订单管理列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        phone: null,
        goodsId: null,
        goodsName: null,
        card: null,
        payDate: null,
        total: null,
        way: null,
        zheng: null,
        water: null,
        hun: null,
        cardFront: null,
        cardBack: null,
        allPath: null,
        currentPath: null,
        wxUserId: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getOrder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除订单管理编号为"' + ids + '"的数据项？').then(function () {
        return delOrder(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('car/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    },
    /**
     * 获取车辆商品下拉框数据
     */
    handlerGoodsAllVo() {
      listGoodsAllVo().then(response => {
        this.goodsList = response.data;
      });
    },
    selectWayChanged(value) {
      if (value === '全款') {
        this.form.allPath = JSON.stringify(this.allPathFull);
        this.currentPathList = this.currentPathListFull;
      } else {
        this.form.allPath = JSON.stringify(this.allPathLoan);
        this.currentPathList = this.currentPathListLoan;
      }
      console.log("allPath:", this.form.allPath);
    },
  }
};
</script>
